package com.axun.ckz.mysocketdemo.utils

import android.content.Context
import android.content.SharedPreferences

/**
 * Created by ckz on 2018/5/2.
 */

class UserShare private constructor(private val context: Context) {
    private val preferences: SharedPreferences
    private val editor: SharedPreferences.Editor

    val userId: String
        get() = preferences.getString("userId", "")

    val key: String
        get() = preferences.getString("key", "")

    init {
        preferences = context.getSharedPreferences("USER", Context.MODE_PRIVATE)
        editor = preferences.edit()
    }

    fun saveUser(userId: String) {
        editor.putString("userId", userId)

        editor.commit()
    }

    fun clearUser() {
        editor.clear()
    }

    companion object {
        var instance: UserShare? = null

        fun getInstance(context: Context): UserShare? {
            if (instance == null) {
                synchronized(UserShare::class.java) {
                    if (instance == null) {
                        instance = UserShare(context)
                    }
                }
            }
            return instance
        }
    }
}
